<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>推文辅助</title>
    <script src="/public/html/common.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            color: #333;
        }
        header {
            background-color: #333;
            color: #fff;
            padding: 1em 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap; /* 允许换行 */
        }
        header h1 {
            margin: 0;
            font-size: 1.5em;
        }
        .header-buttons button {
                background-color: #007bff;
                color: white;
                border: none;
                padding: 10px 15px;
                border-radius: 5px;
                cursor: pointer;
                font-size: 16px;
                transition: background-color 0.3s ease;
                margin-left: 10px; /* 按钮之间的间隔 */
                height: 40px; /* 保持按钮高度一致 */
                display: flex;
                align-items: center;
                justify-content: center;
            }
        .search-container {
            display: flex;
            max-width: 600px;
        }

        .search-container input {
            flex-grow: 1;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .search-container button {
            background-color: #28a745;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 5px;
            cursor: pointer;
        }

        .search-container button:hover {
            background-color: #218838;
        }

        .header-buttons {
                display: flex;
                align-items: center;
                height: 40px; /* 保持与搜索框相同的高度 */
            }

        .header-buttons button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.9em;
        }

        .header-buttons button:hover {
                background-color: #0056b3;
            }

            /* 确保搜索框和按钮对齐 */
            .search-container input[type="text"] {
                height: 40px; /* 保持与按钮相同的高度 */
                box-sizing: border-box; /* 包含padding和border在高度内 */
                margin-left: 10px;
            }

            .search-container button {
                height: 40px; /* 保持与搜索框相同的高度 */
                box-sizing: border-box; /* 包含padding和border在高度内 */
            }

        .container {
            width: 90%;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .project-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .project-card {
            border: 1px solid #ddd;
            padding: 10px 15px;
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
            flex-wrap: wrap; /* Allow wrapping on smaller screens */
        }
        .project-info {
            display: flex;
            align-items: center;
            flex-grow: 1;
            flex-wrap: wrap;
        }
        .project-info h3 {
            margin: 0;
            color: #007bff;
            font-size: 1.1em;
            margin-right: 15px;
        }
            .project-info p {
                margin: 0;
                white-space: nowrap; /* Prevent text from wrapping */
                overflow: hidden;
                text-overflow: ellipsis;
                padding-right: 10px; /* Add some padding to the right of the text */
            }

            /* Generic Modal Styles */
            .modal {
                display: none; /* Hidden by default */
                position: fixed; /* Stay in place */
                z-index: 1000; /* Sit on top */
                left: 0;
                top: 0;
                width: 100%; /* Full width */
                height: 100%; /* Full height */
                overflow: auto; /* Enable scroll if needed */
                background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
                justify-content: center;
                align-items: center;
            }

            .modal-content {
                background-color: #fefefe;
                margin: auto;
                padding: 20px;
                border: 1px solid #888;
                width: 80%;
                max-width: 900px;
                border-radius: 8px;
                position: relative;
                box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
                animation-name: animatetop;
                animation-duration: 0.4s;
            }

            /* Add Animation */
            @keyframes animatetop {
                from {top: -300px; opacity: 0}
                to {top: 0; opacity: 1}
            }

            .close-button {
                color: #aaa;
                float: right;
                font-size: 28px;
                font-weight: bold;
                position: absolute;
                right: 20px;
                top: 10px;
            }

            .close-button:hover,
            .close-button:focus {
                color: black;
                text-decoration: none;
                cursor: pointer;
            }

            /* Responsive adjustments for modal */
            @media (max-width: 768px) {
                .modal-content {
                    width: 95%;
                    padding: 15px;
                }
            }
        .project-status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 8px;
        }
        .status-0 {
            background-color: #28a745; /* Success */
        }
        .status-1 {
            background-color: #ffc107; /* Pending/Warning */
        }
        .status-2 {
            background-color: #dc3545; /* Error */
        }
        .project-actions {
            display: flex;
            gap: 8px;
            margin: 10px; /* For mobile layout */
            flex-direction: column;
        }
        .project-actions button,
        .download-draft-btn {
            padding: 6px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.85em;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 5px;
        }
        .detail-btn {
            background-color: #007bff;
            color: white;
        }
        .delete-btn {
            background-color: #dc3545;
            color: white;
        }
        .download-draft-btn {
            background-color: #6c757d;
            color: white;
        }
        footer {
            text-align: center;
            padding: 20px;
            color: #777;
            background-color: #eee;
            margin-top: 20px;
        }

        .text-content-container {
            margin-top: 10px;
            width: 100%;
        }

        .project-text {
            width: 100%;
            height: 80px; /* 默认行高 */
            max-height: 150px; /* 最大行高 */
            overflow-y: auto; /* 文本溢出时显示滚动条 */
            resize: none; /* 禁止用户调整大小 */
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 0.9em;
            box-sizing: border-box; /* 包含padding和border在内的宽度 */
            margin-top: 5px;
        }

        /* Responsive adjustments */
        @media (max-width: 768px) {
            header {
                flex-direction: column;
                text-align: center;
                padding: 1em 10px; /* 调整内边距 */
            }
            header h1 {
                margin-bottom: 10px;
            }
            .header-buttons {
                margin-top: 10px;
                width: 100%;
                justify-content: center;
            }
            .header-buttons button {
                width: 48%; /* 按钮宽度调整 */
                margin: 5px 0; /* 调整外边距 */
            }
            .search-container {
                flex-direction: column;
                align-items: center;
                width: 90%;
                margin: 10px auto;
            }
            .search-container input {
                width: 100%;
                margin-bottom: 10px;
            }
            .search-container button {
                width: 100%;
            }
            .project-card {
                flex-direction: column;
                align-items: flex-start;
                padding: 10px;
            }
            .project-info {
                flex-direction: column;
                align-items: flex-start;
                width: 100%;
                margin-bottom: 10px;
            }
            .project-info p {
                margin: 5px 0;
            }
            .project-actions {
                width: 100%;
                justify-content: flex-end;
            }

        }
    </style>
</head>
<body>
    <div id="message-area"></div>
    <header>
        <h1>推文辅助系统</h1>
        <div class="header-buttons">
            <button id="configBtn">配置</button>
            <button id="roleBtn">角色管理</button>
            <button id="newProjectBtn">新建项目</button>
            <div class="search-container">
                <input type="text" id="searchInput" placeholder="搜索项目...">
                <button id="searchBtn">搜索</button>
            </div>
        </div>
    </header>
    <div class="container">
        <h2>项目列表</h2>
        <div id="project-list" class="project-list"></div>

    </div>



    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const projectsContainer = document.getElementById('project-list');
            // 绑定头部按钮事件
            document.getElementById('configBtn').addEventListener('click', function() {
                window.open(`/public/html/settings.html`, '_blank');
            });
            document.getElementById('roleBtn').addEventListener('click', function() {
                window.open(`/public/html/roleList.html`, '_blank');
            });
            document.getElementById('newProjectBtn').addEventListener('click', function() {
                window.open(`/public/html/new_project.html`, '_blank');
            });

            document.getElementById('searchBtn').addEventListener('click', () => {
                const searchTerm = document.getElementById('searchInput').value;
                filterProjects(searchTerm);
            });

            function filterProjects(searchTerm) {
                const projectCards = document.querySelectorAll('.project-card');
                projectCards.forEach(card => {
                    const projectName = card.querySelector('h3').textContent;
                    if (projectName.toLowerCase().includes(searchTerm.toLowerCase())) {
                        card.style.display = '';
                    } else {
                        card.style.display = 'none';
                    }
                });
            }
            // 获取项目列表
            function fetchProjects() {
                fetch('/projects/query') // 假设 API 服务运行在 8080 端口
                    .then(response => response.json())
                    .then(data => {
                        if (!!data) {
                            renderProjects(data);
                        } else {
                            console.error('获取项目失败:', data);
                            projectsContainer.innerHTML = `<p>加载项目失败: ${data}</p>`;
                        }
                    })
                    .catch(error => {
                        console.error('请求项目列表出错:', error);
                        projectsContainer.innerHTML = `<p>请求项目列表出错: ${error.message}</p>`;
                    });
            }

            // 渲染项目列表
            function renderProjects(projects) {
                const projectListHtml = projects.map(project => `
                    <div class="project-card" data-project-id="${project.id}">
                        <div class="project-info">
                            <span class="project-status-indicator status-${project.status}"></span>
                            <h3>${project.name}</h3>
                    <p><strong>创建时间:</strong> ${new Date(project.createTime).toLocaleString()}</p>
                    <p><strong>状态:</strong> ${project.status}</p>
                    <div class="text-content-container">
                        <strong>文本内容:</strong>
                        <textarea class="project-text" readonly>${project.textContent}</textarea>
                    </div>
                </div>
                        <div class="project-actions">
                            <button class="detail-btn" data-project-id="${project.id}">任务详情</button>
                            <button class="delete-btn" data-project-id="${project.id}">删除项目</button>
                            ${project.draft ? `<a href="${project.draft}" target="_blank" class="download-draft-btn">下载草稿</a>` : ''}
                        </div>
                    </div>
                `).join('');
                projectsContainer.innerHTML = projectListHtml;

                // 添加事件监听器
                document.querySelectorAll('.delete-btn').forEach(button => {
                    button.addEventListener('click', function() {
                        const projectId = this.dataset.projectId;
                        if (confirm(`确定要删除项目 ${projectId} 吗？`)) {
                            deleteProject(projectId);
                        }
                    });
                });

                document.querySelectorAll('.detail-btn').forEach(button => {
                    button.addEventListener('click', function() {
                        const projectId = this.dataset.projectId;
                        window.open(`/public/html/project_detail_comfyui.html?projectId=${projectId}`, '_blank');
                    });
                });
            }

            // 删除项目
            function deleteProject(projectId) {
                fetch('/project/delete', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ project_id: parseInt(projectId) })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 0) {
                        showMessage('项目删除成功！', 'success');
                        fetchProjects(); // 重新加载项目列表
                    } else {
                        showMessage('删除项目失败: ' + data.msg, 'error');
                    }
                })
                .catch(error => {
                    console.error('删除项目出错:', error);
                    showMessage('删除项目出错: ' + error.message, 'error');
                });
            }

            // 初始加载项目
            fetchProjects();
        });
    </script>
</body>
</html>
